package com.zhu.ttwords.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.zhu.ttwords.R;
import com.zhu.ttwords.presenter.LoginPresenter;


public class LoginActivity extends ActionBarActivity implements ILoginActivity {
    public static final Long loadTime = 2000l;
    SharedPreferences sp;
    LoginPresenter presenter;
    //views
    Animation rotateAnimation;
    ViewGroup loadGrouop;
    ImageView img_loading;
    Button btn_login;
    Button btn_regist;
    EditText username;
    EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        rotateAnimation = AnimationUtils.loadAnimation(this,R.anim.loading_animation);
        loadGrouop = (ViewGroup) this.findViewById(R.id.activity_login_group);
        img_loading = (ImageView) this.findViewById(R.id.activity_login_loading);
        btn_login = (Button) this.findViewById(R.id.activity_login_login);
        btn_regist = (Button) this.findViewById(R.id.activity_login_regist);
        username = (EditText) this.findViewById(R.id.activity_login_user);
        password = (EditText) this.findViewById(R.id.activity_login_password);

        img_loading.startAnimation(rotateAnimation);
        presenter = new LoginPresenter(this,this);
        presenter.init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    public void login(View v) {
        presenter.login();
    }

    public void regist(View v) {
        presenter.regist();
    }

    public void forgotPassword(View v) {
        presenter.forgetPassword();
    }

    @Override
    public void loadComplate() {
        loadGrouop.setVisibility(View.GONE);
    }

    @Override
    public void loginSuccess() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void loginFailed() {

    }

}
